//
//  WFTabBarItem.swift
//  WangFangWeiBo
//
//  Created by 王方 on 16/7/12.
//  Copyright © 2016年 wf. All rights reserved.
//

import UIKit

class WFTabBarItem: UITabBarItem {
    
    //重写这个控件的set属性
    override var badgeValue:String?{
        didSet {
            //  1. 直接去我们主窗口的根视图控制器
            //let tabBarController = UIApplication.sharedApplication().keyWindow?.rootViewController as? UITabBarController
            //  2. 通过kvc 获取对应_target对象也就是我们的WFMainTBC
            let tabBarController = valueForKey("_target") as? UITabBarController
            
            for tabbarChild in  tabBarController!.tabBar.subviews {
                
                if tabbarChild.isKindOfClass(NSClassFromString("UITabBarButton")!){
                   
                    for tabbarButtonChild in tabbarChild.subviews{
                        
                        if tabbarButtonChild .isKindOfClass(NSClassFromString("_UIBadgeView")!){
                        
                            for badgeViewChild in tabbarButtonChild.subviews{
                                if badgeViewChild.isKindOfClass(NSClassFromString("_UIBadgeBackground")!){
                                   print("老夫终于找到你!kimoji")
                                    var count:UInt32 = 0
                                    let ivars = class_copyIvarList(NSClassFromString("_UIBadgeBackground")!, &count)
                                    for i in 0..<count {
                                        //这里开始就要用到运行时的方法了
                                        //获取指定位置的成员
                                        let ivar = ivars[Int(i)]
                                        let name = ivar_getName(ivar)
                                        let type = ivar_getTypeEncoding(ivar)
                                        
                                        //把获取到的成员名字转成字符串
                                        let nameStr = String(CString: name, encoding: NSUTF8StringEncoding)
                                        let typeStr = String(CString: type, encoding: NSUTF8StringEncoding)
                                        print(nameStr)
                                        print(typeStr)
                                        /**
                                        *   打印结果:
                                            Optional("_image")
                                            Optional("@\"UIImage\"")
                                        */
                                        //说明是图片属性
                                        
                                        if nameStr == "_image"{
                                            badgeViewChild.setValue(UIImage(named: "main_badge"), forKey: "_image")
                                        }
                                        
                                    }

                                    
                                }
                            }
                        }
                    }
                
                }
            }

        }
    }
}
